package samples.d2.parallels.trames 
{
	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.filters.GlowFilter;
	import flash.utils.setInterval;
	import samples.BaseSample;
	import shapes.shapes2D.Dot;
	import shapes.shapes2D.Line;
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Trame4 extends BaseSample
	{
		
		private var d0:Dot;
		private var d1:Dot;
		private var line:Line;
		private var lines:Vector.<Line>;
		private var strokesWidth:Vector.<Number>;
		private var strokesAlpha:Vector.<Number>;
		private var angle:Number = 0;
		
		public function Trame4() 
		{
			
			
			filters = [new GlowFilter( 0xFFFFFF, 1, 6, 6, 2, 1 ) ];
			resetHandler();
			interval = setInterval( resetHandler, 30 );
			
		}
		
		private function resetHandler():void
		{
			reset ( 10, 10 );
		}
		
		private function reset( horizontal:int = 10, vertical:int = 10 ):void
		{
			lines = new Vector.<Line>();
			if ( strokesWidth == null ) strokesWidth = new Vector.<Number>();
			if ( strokesAlpha == null ) strokesAlpha = new Vector.<Number>();
			
			var width:int = 300;
			var height:int = 300;
			
			var offset:int = 0;
			var offsetX:int = 0;
			var offsetY:int = 0;
			
			var i:int;
			for ( i = 0; i <= horizontal; i++ )
			{
				
				offsetX = -width / 2 + ( width / horizontal ) * i;
				
				offset = Math.cos( angle ) * 100;
				angle += Math.PI / 2080;
				
				d0 = new Dot( sw / 2 + offsetX + offset, sh / 2 - height / 2 );
				d1 = new Dot( sw / 2 + offsetX - offset, sh / 2 + height / 2 );
				line = new Line( d0, d1 );
				
				if( strokesWidth.length <= horizontal ) strokesWidth.push( Math.random() * 20 );
				if( strokesAlpha.length <= horizontal ) strokesAlpha.push( Math.random() + .1 );
				lines.push( line );
				
			}
			
			for ( i = 0; i <= vertical; i++ )
			{
				
				offsetY = -height / 2 + ( height / vertical ) * i;
				
				offset = Math.cos( angle ) * 100;
				angle += Math.PI / 2080;
				
				d0 = new Dot( sw/2 - width / 2, sh / 2 + offsetY  - offset);
				d1 = new Dot( sw/2 + width / 2, sh / 2 + offsetY  + offset);
				line = new Line( d0, d1 );
				if( strokesWidth.length <= horizontal + vertical + 1 ) strokesWidth.push( Math.random() * 20 );
				if( strokesAlpha.length <= horizontal + vertical + 1 ) strokesAlpha.push( Math.random() + .1 );
				
				lines.push( line );
				
			}
			render();
		}
		
		private function render():void
		{
			graphics.clear();
			//for each( line in lines )
			for ( var i:int = 0; i < lines.length; i++ )
			{
				line = lines[ i ];
				graphics.lineStyle( 0, color );
				line.d0.draw( graphics );
				line.d1.draw( graphics );
				graphics.lineStyle( strokesWidth[ i ], color, strokesAlpha[ i ], false, LineScaleMode.NORMAL, CapsStyle.SQUARE, JointStyle.MITER );
				line.draw( graphics );
			}	
		}
	}

}